﻿using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using System;

public class PullingItemController : MonoBehaviour , IPointerClickHandler
{
    public GameObject PullingItemPrefab;
    private Text _text;
    private GameObject _pullingItemGrid;
    private Action EnableOnClick;

    void Awake()
    {
        Init();
    }

    void Init()
    {
        _text = transform.GetChild(0).GetComponent<Text>();
        _pullingItemGrid = transform.GetChild(1).gameObject;
        EnableOnClick = ShowOrCloseGrid;
        //根据需要生成下拉框
        InitPullingItem();
    }

    void InitPullingItem()
    {
        for (int i = 0; i < 4; i++)
        {
            GameObject tempItem = (GameObject) Instantiate(PullingItemPrefab);
            tempItem.transform.SetParent(_pullingItemGrid.transform);
            tempItem.transform.localScale = Vector3.one;
            tempItem.GetComponent<PullingItemScript>().Init("Text" + i, ChangeTextClickItem);
        }
    }

    void ChangeTextClickItem(Text changeText)
    {
        _text.text = changeText.text;
        ShowOrCloseGrid();
    }

    void ShowOrCloseGrid()
    {
        _pullingItemGrid.SetActive(!_pullingItemGrid.activeSelf);
    }

    public void OnPointerClick(PointerEventData eventData)
    {
        Debug.Log(_pullingItemGrid.name);
        if (EnableOnClick != null) EnableOnClick();
    }

}
